<template>
  <el-dialog 
    :model-value="dialogFormVisible" 
    close-on-press-escape 
    close-on-click-modal 
    width="30%"
    :title="dialogTitle"
    @close="handleClose"
  >
    <el-form ref="formRef" :model="form" label-width="70px" :rules="rules">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">关闭</el-button>
        <el-button type="primary" @click="handleConfirm">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup>
import { defineEmits,ref,defineProps } from 'vue'
import { addUsers } from '@/api'
import { ElMessage } from 'element-plus'
const emits = defineEmits(['update:modelValue','initUserList'])
const handleClose = () => {
  emits('update:modelValue',false)
}
const handleConfirm = () => {
  formRef.value.validate(async (valid) => {
    if (valid) {
      await addUsers(form.value)
      ElMessage({
        message: '添加成功',
        type: 'success',
      })
      emits('initUserList')
      handleClose()
    } else {
      return false;
    }
  });
}
const formRef = ref(null)
const form = ref({
  username:'',
  password:'',
  email:'',
})
const rules = ref({
  username:[
    { required: true, message: '请输入11位账号' },
    { min: 11, max: 11, message: '号码只能为11位' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    {
      type: 'email',
      message: '请输入正确格式的邮箱',
      trigger: ['blur', 'change'],
    },
  ],

})
defineProps({
  dialogTitle:{
    type: String,
    default: '',
    required: true
  }
})
</script>
<style scoped lang="scss">

</style>